package com.anet;

import android.util.Log;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.apache.http.entity.StringEntity;

public class WebUtils {

    private static HttpUtils mUtil;

    static {
        mUtil = new HttpUtils();
    }

    public static void GET(String url, final OnDataListener mCb) {
        mUtil.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onLoading(long total, long current, boolean isUploading) {
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String ret = responseInfo.result;
                mCb.onSuccess(ret);
            }

            @Override
            public void onStart() {
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Log.e("aaa",error.toString()+"   -"+msg);
                mCb.onFailed(msg);
            }
        });
    }

    public static void POST(String url, String para, final OnDataListener mCb) {
        RequestParams params = new RequestParams();
        params.addHeader("Charset", "UTF-8");
        params.addHeader("Connection", "Keep-Alive");
        params.addHeader("Content-Type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(para, "UTF-8"));
        } catch (Exception e) {
        }
        mUtil.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onLoading(long total, long current, boolean isUploading) {
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String ret = responseInfo.result;
                mCb.onSuccess(ret);
            }

            @Override
            public void onStart() {
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                mCb.onFailed(msg);
            }
        });
    }


    public static void POST_TEST() {
        Log.e("aaa", "======================================");

        String urls = "https://passport.lenovo.com/interserver/authen/1.2/st/user/getustbypwd?msisdn=13681360901";
        RequestParams params = new RequestParams();
        params.addHeader("Charset", "UTF-8");
        params.addHeader("Connection", "Keep-Alive");
        params.addHeader("Content-Type", "application/x-www-form-urlencoded");
        params.addBodyParameter("areacode", "+86");
        params.addBodyParameter("realm", "lenovo.meeting.launcher");
        params.addBodyParameter("password", "cmz151515");
        params.addBodyParameter("source", "android:lenovo.meeting.launcher-1.1");
        params.addBodyParameter("deviceidtype", "imei");
        params.addBodyParameter("lang", "en");
        params.addBodyParameter("deviceid", "12341234321");

//        String urls = "https://passport.lenovo.com/interserver/authen/1.2/st/user/getustbypwd?msisdn=18523381596";
//        RequestParams params = new RequestParams();
//        params.addHeader("Charset", "UTF-8");
//        params.addHeader("Connection", "Keep-Alive");
//        params.addHeader("Content-Type", "application/x-www-form-urlencoded");
//        params.addBodyParameter("areacode","+86");
//        params.addBodyParameter("realm","lenovo.meeting.launcher");
//        params.addBodyParameter("password","igrs123456");
//        params.addBodyParameter("source","android:lenovo.meeting.launcher-1.1");
//        params.addBodyParameter("deviceidtype","imei");
//        params.addBodyParameter("lang","en");
//        params.addBodyParameter("deviceid","12341234329");
        mUtil.send(HttpRequest.HttpMethod.POST, urls, params, new RequestCallBack<String>() {
            @Override
            public void onLoading(long total, long current, boolean isUploading) {
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String ret = responseInfo.result;
                Log.e("net", "AAA==  " + ret);
            }

            @Override
            public void onStart() {
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Log.e("net", "==  FAILED      错误码:   " + msg);
            }
        });
    }

    public interface OnDataListener {
        void onSuccess(String ret);

        void onFailed(String error);
    }
}
